package com.site.blog.my.core.controller.admin;

import com.site.blog.my.core.entity.AdminUser;
import com.site.blog.my.core.service.AdminUserService;
import org.apache.tomcat.jni.User;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 13
 * @qq交流群 796794009
 * @email 2449207463@qq.com
 * @link http://13blog.site
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminUserService adminUserService;



    @GetMapping({"/login"})
    public String login() {
        return "admin/login";
    }

    @PostMapping(value = "/login")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        @RequestParam("verifyCode") String verifyCode,
                        HttpSession session) {
        if (StringUtils.isEmpty(verifyCode)) {
            session.setAttribute("errorMsg", "验证码不能为空");
            return "admin/login";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "admin/login";
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg", "验证码错误");
            return "admin/login";
        }
        AdminUser adminUser = adminUserService.login(userName, password);
        if (adminUser != null) {
            session.setAttribute("loginUser", adminUser.getNickName());
            session.setAttribute("loginUserId", adminUser.getAdminUserId());
            //session过期时间设置为7200秒 即两小时
            session.setMaxInactiveInterval(60 * 60 * 2);
            return "redirect:/admin/index";
        } else {
            session.setAttribute("errorMsg", "登陆失败");
            return "admin/login";
        }
    }

    @GetMapping("/profile")
    public String profile(HttpServletRequest request){
        // 拿到用户id，之前存在session中的
        int id = (int) request.getSession().getAttribute("loginUserId");
        // 查询出用户原始信息，并存到session中
        AdminUser user = adminUserService.getUserDetailById(id);
        // session过期
        if (user == null) return "admin/login";

        // 回显的信息和导航栏高亮
        request.setAttribute("path","profile");
        request.setAttribute("loginUserName",user.getLoginUserName());
        request.setAttribute("nickName",user.getNickName());
        return "admin/profile";
    }

    @PostMapping("/profile/password")
    @ResponseBody
    public String password(@RequestParam(value = "originalPassword") String originalPassword,
                           @RequestParam(value = "newPassword") String newPassword,
                           HttpSession session){
        // 验证参数
        if (StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(newPassword)){
            return "参数不能为空~";
        }
        // 拿到userId
        int id = (int) session.getAttribute("loginUserId");
        // 调用updatePassword，判断成功与否
        if (adminUserService.updatePassword(id,originalPassword,newPassword)){
            // 清空session中的内容，重新登录。前端控制跳转逻辑。
            session.removeAttribute("loginUser");
            session.removeAttribute("loginUserId");
            session.removeAttribute("errorMsg");
            return "success";
        }
        return "修改失败~";
    }

    @PostMapping("/profile/name")
    @ResponseBody
    public String name(@RequestParam(value = "loginUserName") String loginUserName,
                       @RequestParam(value = "nickName") String nickName,
                       HttpSession session){
        if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)){
            return "参数错误~";
        }
        int id = (int) session.getAttribute("loginUserId");

        if (adminUserService.updateName(id,loginUserName,nickName)){
            return "success";
        }else{
            return "修改失败~";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "admin/login";
    }

    @GetMapping({"", "/", "/index", "/index.html"})
    public String index() {
        return "admin/index";
    }

}
